import apiConfig from '@/config.js'
const baseUrl = process.env.NODE_ENV === "development" ? apiConfig.dev.baseUrl : apiConfig.prod.baseUrl

const requst = (options = {}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url:baseUrl+options.url || '',
			method: options.method || 'GET',
			data:options.data ||{},
			header: options.header || {"Content-Type": "application/json", token: uni.getStorageSync('xm-user')?.token}
		}).then(res =>{
			let{ data } = res
			if (data.code === '401') {
				uni.navigateTo({
					url:'/pages/login/login'
				})
			}
			resolve(data);
		}).catch(error => {
			reject(error)
		})
	});
}

const get = (url, data, option = {})=> {
	options.method = 'GET'
	options.data = data
	options.url = url
	return requst(options)
}

const post = (url, data, option = {})=> {
	options.method = 'POST'
	options.data = data
	options.url = url
	return requst(options)
}

const put = (url, data, option = {})=> {
	options.method = 'PUT'
	options.data = data
	options.url = url
	return requst(options)
}

const del = (url, data, option = {})=> {
	options.method = 'DELETE'
	options.data = data
	options.url = url
	return requst(options)
}

export default {
	requst,
	get,
	post,
	put,
	del
}
